package com.lnlyj.media.ui;

import com.lnlyj.media.ResultData;
import com.lnlyj.media.data.MediaItem;

import java.util.ArrayList;
import java.util.List;

/**
 * wanglei 2020-04-17
 */
public class SelectionManager {

    private List<MediaItem> mediaItems = new ArrayList<>();
    private boolean fullImage = false;

    private static class Instance {
        public static SelectionManager manager = new SelectionManager();
    }

    public static SelectionManager getInstance() {
        return Instance.manager;
    }

    private SelectionManager() {
    }

    public boolean isFullImage() {
        return fullImage;
    }

    public void setFullImage(boolean fullImage) {
        this.fullImage = fullImage;
    }

    public void toggle(MediaItem item) {
        if (!mediaItems.contains(item)) {
            mediaItems.add(item);
        } else {
            mediaItems.remove(item);
        }
    }

    public void add(MediaItem item) {
        if (!mediaItems.contains(item)) {
            mediaItems.add(item);
        }
    }

    public void remove(MediaItem item) {
        if (mediaItems.contains(item)) {
            mediaItems.remove(item);
        }
    }

    public boolean contains(MediaItem item) {
        return mediaItems.contains(item);
    }

    public int getSize() {
        return mediaItems.size();
    }

    public List<MediaItem> getMediaItems() {
        return mediaItems;
    }

    public ArrayList<ResultData> getResultData() {
        ArrayList<ResultData> list = new ArrayList<>();
        for (MediaItem item : mediaItems) {
            list.add(new ResultData(item, isFullImage()));
        }
        return list;
    }

    public void clear() {
        mediaItems.clear();
        fullImage = false;
    }

    public int getIndex(MediaItem item) {
        if (mediaItems.contains(item)) {
            return mediaItems.indexOf(item) + 1;
        }
        return -1;
    }
}
